package com.zyd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyd.controller.result.Result;
import com.zyd.dto.EmployeeQueryDto;
import com.zyd.entity.Employee;
import com.zyd.handle.GlobalExceptionHandle;
import com.zyd.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("employee")
public class EmployeeController_bak {
    @Autowired
    private IEmployeeService iEmployeeService;

    // 查询所有Employee信息
    @ApiOperation("查询所有employee信息")
    @GetMapping("/listAll")
    public List<Employee> listAll(){
        List<Employee> employeeList =  iEmployeeService.list();
        return employeeList;
    }

    //添加Employee信息
    @ApiOperation("添加管理员接口")
    @PostMapping("/add")
    public Result addEmployee(@RequestBody Employee employee){
        System.out.println(employee);
        //其业务
        boolean res = iEmployeeService.save(employee);
        if(res){
            Result.ok();
        } else {
            Result.fail();
        }
        return Result.ok();
    }

    //根据id获取Employee信息
    @ApiOperation("根据id查询Employee接口")
    @GetMapping("/list/{id}")
    public Result getEmployeeById(@PathVariable Integer id){
        return Result.ok(iEmployeeService.getById(id));
    }

    //根据id修改Employee信息
    @ApiOperation("根据id修改Employee接口")
    @PutMapping("/update")
    public Result updateEmployeeById(@RequestBody Employee Employee){
        iEmployeeService.updateById(Employee);
        return Result.ok();
    }

    //根据id删除Employee信息
    @ApiOperation("根据id删除Employee接口")
    @DeleteMapping("/delete/{id}")
    public Result deleteEmployeeById(@PathVariable int id){
        iEmployeeService.removeById(id);
        return Result.ok();
    }

    //条件分页查询
    @ApiOperation("条件分页查询")
    @GetMapping("/page/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, EmployeeQueryDto EmployeeQueryDto){
        IPage<Employee> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<Employee>();
        queryWrapper.like(Strings.isNotEmpty(EmployeeQueryDto.getName()),"name",EmployeeQueryDto.getName());
        queryWrapper.like(Strings.isNotEmpty(EmployeeQueryDto.getPhone()),"description",EmployeeQueryDto.getPhone());
        iEmployeeService.page(page, queryWrapper);
        return Result.ok(page);
    }

    @ApiOperation("处理异常接口")
    @GetMapping("/excetion")
    public Result handleException() {
        try {
            int a = 10;
            int b = 0;
            double c = a/b;
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GlobalExceptionHandle();
        }
    }

}
